#include "CanTp_PBcfg.h"

{%- for configuration in configurations %}
    {%- for channel in configuration.channels %}
        {%- if channel.receivers is defined %}
            {%- for receiver in channel.receivers %}
                {%- if receiver.source_address is defined %}
#ifndef CANTP_N_SA_{{'%02X' % receiver.source_address}}

#define CANTP_N_SA_{{'%02X' % receiver.source_address}} ({{'0x%02Xu' % receiver.source_address}})

#define CanTp_START_SEC_CONST_8
#include "CanTp_MemMap.h"

static const CanTp_NSaType CanTp_NSa{{'%02X' % receiver.source_address}} = {
    CANTP_N_SA_{{'%02X' % receiver.source_address}}
};

#define CanTp_STOP_SEC_CONST_8
#include "CanTp_MemMap.h"

#endif /* #ifndef CANTP_N_SA_{{'%02X' % receiver.source_address}} */
                {% endif %}
                {%- if receiver.target_address is defined %}
#ifndef CANTP_N_TA_{{'%02X' % receiver.target_address}}

#define CANTP_N_TA_{{'%02X' % receiver.target_address}} ({{'0x%02Xu' % receiver.target_address}})

#define CanTp_START_SEC_CONST_8
#include "CanTp_MemMap.h"

static const CanTp_NTaType CanTp_NTa{{'%02X' % receiver.target_address}} = {
    CANTP_N_TA_{{'%02X' % receiver.target_address}}
};

#define CanTp_STOP_SEC_CONST_8
#include "CanTp_MemMap.h"

#endif /* #ifndef CANTP_N_TA_{{'%02X' % receiver.target_address}} */
                {% endif %}
                {%- if receiver.address_extension is defined %}
#ifndef CANTP_N_AE_{{'%02X' % receiver.address_extension}}

#define CANTP_N_AE_{{'%02X' % receiver.address_extension}} ({{'0x%02Xu' % receiver.address_extension}})

#define CanTp_START_SEC_CONST_8
#include "CanTp_MemMap.h"

static const CanTp_NAeType CanTp_NAe{{'%02X' % receiver.address_extension}} = {
    CANTP_N_AE_{{'%02X' % receiver.address_extension}}
};

#define CanTp_STOP_SEC_CONST_8
#include "CanTp_MemMap.h"

#endif /* #ifndef CANTP_N_AE_{{'%02X' % receiver.address_extension}} */
                {% endif -%}
            {% endfor -%}
        {% endif -%}
        {%- if channel.transmitters is defined %}
            {%- for transmitter in channel.transmitters %}
               {%- if transmitter.source_address is defined %}
#ifndef CANTP_N_SA_{{'%02X' % transmitter.source_address}}

#define CANTP_N_SA_{{'%02X' % transmitter.source_address}}

#define CanTp_START_SEC_CONST_8
#include "CanTp_MemMap.h"

static const CanTp_NSaType CanTp_NSa{{'%02X' % transmitter.source_address}} = {
   {{'0x%02Xu' % transmitter.source_address}}
};

#define CanTp_STOP_SEC_CONST_8
#include "CanTp_MemMap.h"

#endif /* #ifndef CANTP_N_SA_{{'%02X' % transmitter.source_address}} */
               {% endif %}
               {%- if transmitter.target_address is defined %}
#ifndef CANTP_N_TA_{{'%02X' % transmitter.target_address}}

#define CANTP_N_TA_{{'%02X' % transmitter.target_address}}

#define CanTp_START_SEC_CONST_8
#include "CanTp_MemMap.h"

static const CanTp_NTaType CanTp_NTa{{'%02X' % transmitter.target_address}} = {
   {{'0x%02Xu' % transmitter.target_address}}
};

#define CanTp_STOP_SEC_CONST_8
#include "CanTp_MemMap.h"

#endif /* #ifndef CANTP_N_TA_{{'%02X' % transmitter.target_address}} */
               {% endif %}
               {%- if transmitter.address_extension is defined %}
#ifndef CANTP_N_AE_{{'%02X' % transmitter.address_extension}}

#define CANTP_N_AE_{{'%02X' % transmitter.address_extension}}

#define CanTp_START_SEC_CONST_8
#include "CanTp_MemMap.h"

static const CanTp_NAeType CanTp_NAe{{'%02X' % transmitter.address_extension}} = {
   {{'0x%02Xu' % transmitter.address_extension}}
};

#define CanTp_STOP_SEC_CONST_8
#include "CanTp_MemMap.h"

#endif /* #ifndef CANTP_N_AE_{{'%02X' % transmitter.address_extension}} */
               {% endif -%}
            {% endfor -%}
        {% endif -%}
    {% endfor -%}
{% endfor -%}

{%- for configuration in configurations %}
    {% set configuration_loop = loop %}
    {%- for channel in configuration.channels %}
        {% set channel_loop = loop %}
        {%- if channel.receivers is defined %}
#define CanTp_START_SEC_CONST_UNSPECIFIED
#include "CanTp_MemMap.h"

static const CanTp_RxNSduType CanTp_RxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}[] = {
            {%- for receiver in channel.receivers %}
    {
        {{'0x%04Xu' % receiver.network_service_data_unit_identifier}},
                {%- if receiver.source_address is defined %}
        &CanTp_NSa{{'%02X' % receiver.source_address}},
                {%- else %}
        NULL_PTR,
                {%- endif %}
                {%- if receiver.target_address is defined %}
        &CanTp_NTa{{'%02X' % receiver.target_address}},
                {%- else %}
        NULL_PTR,
                {%- endif %}
                {%- if receiver.address_extension is defined %}
        &CanTp_NAe{{'%02X' % receiver.address_extension}},
                {%- else %}
        NULL_PTR,
                {%- endif %}
        {{'0x%02Xu' % receiver.block_size}},
        (uint32)((ieee_float){{'%f' % receiver.N_Ar_timeout}} * (ieee_float)1000000),
        (uint32)((ieee_float){{'%f' % receiver.N_Br_timeout}} * (ieee_float)1000000),
        (uint32)((ieee_float){{'%f' % receiver.N_Cr_timeout}} * (ieee_float)1000000),
        {{'0x%04Xu' % receiver.wait_for_transmission_maximum}},
        (uint32)((ieee_float){{'%f' % receiver.minimum_separation_time}} * (ieee_float)1000000),
        {{receiver.addressing_format}},
        {% if receiver.enable_padding %}CANTP_ON{% else %}CANTP_OFF{% endif %},
        {{receiver.communication_type}},
        {{'0x%04Xu' % receiver.network_service_data_unit_reference}}
    },
            {%- endfor %}
};

#define CanTp_STOP_SEC_CONST_UNSPECIFIED
#include "CanTp_MemMap.h"
        {%- endif %}

        {%- if channel.transmitters is defined %}
#define CanTp_START_SEC_CONST_UNSPECIFIED
#include "CanTp_MemMap.h"

static const CanTp_TxNSduType CanTp_TxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}[] = {
            {%- for transmitter in channel.transmitters %}
    {
        {{'0x%04Xu' % transmitter.network_service_data_unit_identifier}},
                {%- if transmitter.source_address is defined %}
        &CanTp_NSa{{'%02X' % transmitter.source_address}},
                {%- else %}
        NULL_PTR,
                {%- endif %}
                {%- if transmitter.target_address is defined %}
        &CanTp_NTa{{'%02X' % transmitter.target_address}},
                {%- else %}
        NULL_PTR,
                {%- endif %}
                {%- if transmitter.address_extension is defined %}
        &CanTp_NAe{{'%02X' % transmitter.address_extension}},
                {%- else %}
        NULL_PTR,
                {%- endif %}
        (uint32)((ieee_float){{'%f' % transmitter.N_As_timeout}} * (ieee_float)1000000),
        (uint32)((ieee_float){{'%f' % transmitter.N_Bs_timeout}} * (ieee_float)1000000),
        (uint32)((ieee_float){{'%f' % transmitter.N_Cs_timeout}} * (ieee_float)1000000),
        {% if transmitter.enable_transmit_cancellation %}TRUE{% else %}FALSE{% endif %},
        {{transmitter.addressing_format}},
        {% if transmitter.enable_padding %}CANTP_ON{% else %}CANTP_OFF{% endif %},
        {{transmitter.communication_type}},
        {{'0x%04Xu' % transmitter.network_service_data_unit_reference}}
    },
            {%- endfor %}
};

#define CanTp_STOP_SEC_CONST_UNSPECIFIED
#include "CanTp_MemMap.h"
        {%- endif %}
    {%- endfor %}
{%- endfor %}

{% for configuration in configurations %}
{% set configuration_loop = loop %}
#define CanTp_START_SEC_CONST_UNSPECIFIED
#include "CanTp_MemMap.h"

static const CanTp_ChannelType CanTp_ChannelConfig{{'%02X' % configuration_loop.index0}}[] = {
    {%- for channel in configuration.channels %}
    {%- set channel_loop = loop %}
    {
        {
        {%- if channel.receivers is defined %}
            &CanTp_RxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}[0x00u],
            sizeof(CanTp_RxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}) / sizeof(CanTp_RxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}[0x00u]),
        {%- else %}
            NULL_PTR,
            0x00u,
        {%- endif %}
        {%- if channel.transmitters is defined %}
            &CanTp_TxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}[0x00u],
            sizeof(CanTp_TxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}) / sizeof(CanTp_TxNSduConfig{{'%02X' % configuration_loop.index0}}Channel{{'%02X' % channel_loop.index0}}[0x00u])
        {%- else %}
            NULL_PTR,
            0x00u
        {%- endif %}
        },
        {{channel.mode}}
    },
    {%- endfor %}
};

#define CanTp_STOP_SEC_CONST_UNSPECIFIED
#include "CanTp_MemMap.h"
{%- endfor %}

const CanTp_ConfigType CanTp_Config[{{'0x%02Xu' % configurations|length}}] =
{
{%- for configuration in configurations %}
    {
        ((uint32)((ieee_float){{configuration.main_function_period}} * (ieee_float)1000000)),
        sizeof(CanTp_ChannelConfig{{'%02X' % loop.index0}}) / sizeof(CanTp_ChannelConfig{{'%02X' % loop.index0}}[0x00u]),
        &CanTp_ChannelConfig{{'%02X' % loop.index0}}[0x00u],
        {{configuration.padding_byte}},
    },
{%- endfor %}
};

